package com.wanhe.base.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

//import android.support.v7.widget.RecyclerView;

/**
 * 
 * @ClassName: ViewHolderUtil 
 * @Description: viewHolder工具类
 * @author chaoxunYang
 * @date 2016年06月27日 上午10:31:02
 */
public class ViewHolderUtil {
    protected final SparseArray<View> mViews = new SparseArray<View>();
    protected View mConvertView;
    protected Context mContext;
    protected int mPosition;
//    protected RecyclerView mRecyclerView;
    protected ViewGroup mAdapterView;
    protected Object mObj;

    public ViewHolderUtil(ViewGroup adapterView, View convertView) {
        this.mAdapterView = adapterView;
        this.mConvertView = convertView;
        this.mContext = convertView.getContext();
    }
//
//    public ViewHolderUtil(RecyclerView recyclerView, View convertView) {
//        this.mRecyclerView = recyclerView;
//        this.mConvertView = convertView;
//        this.mContext = convertView.getContext();
//    }



    @SuppressWarnings("unchecked")
	public <T extends View> T getView(int viewId) {
        View view = (View)this.mViews.get(viewId);
        if(view == null) {
            view = this.mConvertView.findViewById(viewId);
            this.mViews.put(viewId, view);
        }

        return (T)view;
    }

    public View getConvertView() {
        return this.mConvertView;
    }

    public void setObj(Object obj) {
        this.mObj = obj;
    }

    public Object getObj() {
        return this.mObj;
    }

    public ViewHolderUtil setText(int viewId, String text) {
        TextView view = (TextView)this.getView(viewId);
        view.setText(text);
        return this;
    }

    public ViewHolderUtil setText(int viewId,  int stringResId) {
        TextView view = (TextView)this.getView(viewId);
        view.setText(stringResId);
        return this;
    }

    public ViewHolderUtil setHtml(int viewId, String source) {
        TextView view = (TextView)this.getView(viewId);
        view.setText(Html.fromHtml(source));
        return this;
    }

    public ViewHolderUtil setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable)this.getView(viewId);
        view.setChecked(checked);
        return this;
    }

    public ViewHolderUtil setTag(int viewId, Object tag) {
        View view = this.getView(viewId);
        view.setTag(tag);
        return this;
    }

    public ViewHolderUtil setTag(int viewId, int key, Object tag) {
        View view = this.getView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public ViewHolderUtil setVisibility(int viewId, int visibility) {
        View view = this.getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public ViewHolderUtil setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = (ImageView)this.getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    public ViewHolderUtil setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = (ImageView)this.getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public ViewHolderUtil setTextColorRes(int viewId,  int textColorResId) {
        TextView view = (TextView)this.getView(viewId);
        view.setTextColor(this.mContext.getResources().getColor(textColorResId));
        return this;
    }

    public ViewHolderUtil setTextColor(int viewId, int textColor) {
        TextView view = (TextView)this.getView(viewId);
        view.setTextColor(textColor);
        return this;
    }

    public ViewHolderUtil setBackgroundRes(int viewId, int backgroundResId) {
        View view = this.getView(viewId);
        view.setBackgroundResource(backgroundResId);
        return this;
    }

    public ViewHolderUtil setBackgroundColor(int viewId, int color) {
        View view = this.getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public ViewHolderUtil setBackgroundColorRes(int viewId,  int colorResId) {
        View view = this.getView(viewId);
        view.setBackgroundColor(this.mContext.getResources().getColor(colorResId));
        return this;
    }

    public ViewHolderUtil setImageResource(int viewId, int imageResId) {
        ImageView view = (ImageView)this.getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }


}
